package com.hezi.umeng.utils;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hezi.umeng.R;


/**
 * Created by Kvin on 2017/1/6.
 */
public class NavFooterUtils {
    private static RadioGroup mRadioGroup;
    private static Fragment[] fragments;
    private static OnCheckedChangeListener mOnCheckedChangeListener;
    private static FragmentManager mFragmentManager;

    private static int lastIndex;//record last show page`s index


    /**
     * init
     */
    public static void init(final Context context, Fragment[] fs, int index) {
        final Activity act = (Activity) context;
        mRadioGroup = (RadioGroup) act.findViewById(R.id.nav_group);
        fragments = fs;
        //add content
        mFragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
        mFragmentManager.beginTransaction()
                .add(R.id.base_container, fragments[0])
                .add(R.id.base_container, fragments[1])
                .add(R.id.base_container, fragments[2])
                .add(R.id.base_container, fragments[3])
                .show(fragments[0])
                .hide(fragments[1])
                .hide(fragments[2])
                .hide(fragments[3])
                .commit();
        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
        if (index != 0) {
            ((RadioButton) mRadioGroup.getChildAt(index)).setChecked(true);
            mFragmentManager.beginTransaction().hide(fragments[0]).show(fragments[index]).commit();
            lastIndex = index;
        }

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                int i = radioGroup.indexOfChild(act.findViewById(checkedId));
                ToastUtils.simpleToast(context, "your choice is " + i);
                switch (i) {
                    case 0:
                    case 1:
                        mFragmentManager.beginTransaction().hide(fragments[lastIndex]).show(fragments[i]).commit();
                        lastIndex = i;
                        break;
                    case 3:
                    case 4:
                        mFragmentManager.beginTransaction().hide(fragments[lastIndex]).show(fragments[--i]).commit();
                        lastIndex = i;
                        break;
                }
                if (mOnCheckedChangeListener != null)
                    mOnCheckedChangeListener.onCheckedChanged(radioGroup, checkedId);
            }
        });

    }

    /**
     * set on checked
     *
     * @param position
     */
    public static void setChecked(int position) {
        ((RadioButton) mRadioGroup.getChildAt(position)).setChecked(true);
        mFragmentManager.beginTransaction().hide(fragments[lastIndex]).hide(fragments[position]).commit();
        lastIndex = position;
    }

    /**
     * provide a method for setting actionbar ect
     *
     * @param mOnCheckedChangeListener
     */
    public static void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        NavFooterUtils.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

    /**
     * set on checked changed
     */
    public interface OnCheckedChangeListener {
        void onCheckedChanged(RadioGroup group, int checkedId);
    }
}
